{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Improving Your Code\n",
    "\n",
    "### Problems with your Function\n",
    "\n",
    "Your `probability_uniform` function should work at this point. But, you might run into a couple of problems. \n",
    "\n",
    "1. What happens if you call your function like this: `probability_range(35, 20, 0, 360)` ?\n",
    "2. What happens if you input an angle that is outside the possibilities of the bottle's possible outcomes like probability_range(-25, 390, 0, 360)?\n",
    "3. What if you call your function like this: probability_range('a', 'b', 0, 360)?\n",
    "\n",
    "When writing functions, it's important to think of edge cases or incorrect user input. \n",
    "\n",
    "### Your Task\n",
    "Here are three more task to help deal with these situations:\n",
    "* Make sure the function outputs a valid probability when low_range is greater than high_range. Hint: there is more than one way to do this: \n",
    "  * For example, using absolute value\n",
    " * comparing low_range and high_range  to see which one is greater\n",
    "* Check the inputs to the function to make sure they are not strings. If the user inputted a string, the function should return None. Optionally, print out a message to the user as well explaining why the function put out None. This exercise might seem trivial, but if you try to do something like `'my_string'/2` in python, you will get an error. Debugging the errors and avoiding them is a key programming skill. \n",
    "* Check that the user has only inputted low_range and high_range values that are in between minimum and maximum. If an input is out of the allowed range, return None. Optionally, print out a message to the user as well explaining why the function put out None.\n",
    "\n",
    "Write a function called `probability_range_improved` that takes into account these three tasks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fill out the TODOs. Then run the cells to see if your output is as expected"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def probability_range_improved(low_range, high_range, minimum, maximum):\n",
    "\n",
    "    # TODO: check if any of the inputs are strings.\n",
    "    # hint: the python function isinstance() will be useful\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('Inputs should be numbers not string')\n",
    "        return None\n",
    "    \n",
    "    # TODO check that low_range is between minimum and maximum\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('Your low range value must be between minimum and maximum')\n",
    "        return None\n",
    "        \n",
    "    # TODO check that high_range is between min and max\n",
    "    if ():\n",
    "        # print a message to the user and return none\n",
    "        print('The high range value must be between minimum and maximum')\n",
    "        return None\n",
    "\n",
    "    # TODO: calulate and return the probability \n",
    "    # even if low range is greater than high range\n",
    "    probability = 0\n",
    "    return probability\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below. If there are no AssertionErrors, then your code runs as expected. In Python, assert checks whether a statement\n",
    "resolves to True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "assert probability_range_improved('a', 0, -100, 500) == None\n",
    "assert probability_range_improved(-100, 300, 100, 500) == None\n",
    "assert probability_range_improved(105, 700, 100, 500) == None\n",
    "assert \"{0:.2f}\".format(probability_range_improved(25, 700, 5, 800)) == '0.85'\n",
    "assert \"{0:.2f}\".format(probability_range_improved(700, 25, 5, 800)) == '0.85'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
